//
//  CPSCameraBottomView.m
//  CollTest
//
//  Created by codew on 1/12/22.
//

#import "CPSCameraBottomView.h"
#import "CPSFullScreenCameraPlayView.h"


@interface CPSCameraBottomView ()

@property (weak, nonatomic) IBOutlet UIView *zoomInBgView;
@property (weak, nonatomic) IBOutlet UIView *zoomInView;
@property (weak, nonatomic) IBOutlet UIView *zoomOutView;
@property (strong, nonatomic) CPSFullScreenCameraPlayView *playView;

@end


@implementation CPSCameraBottomView

+ (instancetype)cameraBottomView
{
    CPSCameraBottomView *view = [[[NSBundle mainBundle] loadNibNamed:@"CPSCameraBottomView" owner:nil options:nil] firstObject];
    return view;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.playView.center = CGPointMake(self.zoomInBgView.hx_w / 2, self.zoomInBgView.hx_h / 2);
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self.zoomInBgView addSubview:self.playView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.zoomOutView addGestureRecognizer:tapGesture];
    
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    [self.zoomOutView addGestureRecognizer:longGesture];
}


- (void)longAction:(UILongPressGestureRecognizer *)longGesture {
//    if (self.inTakePictures) {
//        return;
//    }
    if (longGesture.state == UIGestureRecognizerStateBegan) {
        [self.playView clear];
//        CGPoint point = [longGesture locationInView:[UIApplication sharedApplication].keyWindow];
//        self.firstLongGestureLocation = point;
//        self.inTranscribe = YES;
//        self.isAnimation = YES;
        [UIView animateWithDuration:0.2 animations:^{
//            self.backBtn.alpha = 0;
            self.zoomInView.transform = CGAffineTransformMakeScale(1.2, 1.2);
            self.zoomOutView.transform = CGAffineTransformMakeScale(0.6, 0.6);
        } completion:^(BOOL finished) {
//            self.isAnimation = NO;
//            if (self.startTranscribe && self.inTranscribe) {
////                self.backBtn.hidden = YES;
//                self.startTranscribe();
//            }
            
            [self.playView startAnimation];
        }];
    }else if (longGesture.state == UIGestureRecognizerStateChanged) {
//        if (self.isAnimation) {
//            return;
//        }
//        CGPoint point = [longGesture locationInView:[UIApplication sharedApplication].keyWindow];
//        CGFloat margin = self.firstLongGestureLocation.y - point.y;
//        if (margin < 0) {
//            margin = 0;
//        }
//        if (margin < 80.f) {
//            return;
//        }
//        margin -= 80.f;
//        if (self.changedTranscribe) {
//            self.changedTranscribe(margin);
//        }
    }else if (longGesture.state == UIGestureRecognizerStateEnded ||
              longGesture.state == UIGestureRecognizerStateCancelled ||
              longGesture.state == UIGestureRecognizerStateFailed) {
        self.inTranscribe = NO;
        [UIView animateWithDuration:0.2 animations:^{
            self.zoomInView.transform = CGAffineTransformIdentity;
            self.zoomOutView.transform = CGAffineTransformIdentity;
        }];
        [self.playView clear];
//        if (self.endTranscribe) {
//            self.endTranscribe(self.isAnimation);
//        }
//        self.backBtn.alpha = 1;
//        self.backBtn.hidden = NO;
    }
}

- (void)tapAction:(UILongPressGestureRecognizer *)tapGesture {
    
    NSLog(@"");
//    if (self.inTakePictures || self.inTranscribe) {
//        return;
//    }
//    self.inTakePictures = YES;
//    self.backBtn.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.15 animations:^{
        self.zoomOutView.transform = CGAffineTransformMakeScale(0.6, 0.6);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 animations:^{
            self.zoomOutView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
//            self.backBtn.userInteractionEnabled = YES;
        }];
    }];
    if (self.takePictures) {
        self.takePictures();
    }
}


- (CPSFullScreenCameraPlayView *)playView {
    if (!_playView) {
        _playView = [[CPSFullScreenCameraPlayView alloc] initWithFrame:CGRectMake(0, 0, 70, 70) color:UIColor.redColor];
        _playView.duration = 20;//self.manager.configuration.videoMaximumDuration;
    }
    return _playView;
}
@end
